# socket.emit('');
# socket.broadcast.emit('');
# io.sockets.emit('');
express=require 'express'
http=require 'http'
socketio=require 'socket.io'
app=express()
server=http.createServer app
io=socketio.listen server

app.use express.static __dirname+'/view'
server.listen 80
console.log 'Server started on port 80'

genId=->+(''+new Date().getTime()+(Math.random()*1000).toFixed())

class Position
	constructor:(@x,@y)->

class User
	constructor:(@pos)->@id=genId()

users=[]

io.sockets.on 'connection',(socket)->
	socket.on 'login',()->
		console.log 'Someone logined'
		socket.user=user=new User new Position 0,0
		users.push user
		socket.emit 'login success',users,user
		socket.broadcast.emit 'new user',user
	socket.on 'position change',(user)->
		for u,i in users
			if u.id==user.id then users[i]=user
		socket.broadcast.emit 'position update',user
	socket.on 'disconnection',()->
		console.log 'Someone logged out'
		socket.broadcast.emit 'user leave',socket.user
		for u,i in users
			if u.id==user.id then users.splice i;break